home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac-Source 1994 July
/
Mac-Source_July_1994.iso
/
Other Langs
/
mpw yacc ƒ src
/
conflicts.c
< prev
next >
Wrap
C/C++ Source or Header
|
1989-11-19
|
5KB
|
282 lines
#include <stdio.h>
#include "defs.h"
#include "dep.h"
#include "new.h"
#include "files.h"
#include "gram.h"
#include "state.h"
extern int tokensetsize;
extern short *accessing_symbol;
extern shifts **shift_table;
extern unsigned *LA;
extern short *LAruleno;
extern short *lookaheads;
extern char **symbol_name;
char any_conflicts;
char *conflicts;
static unsigned *shiftset;
static unsigned *lookaheadset;
static int src_total;
static int rrc_total;
static int src_count;
static int rrc_count;
initialize_conflicts()
{
register int i;
conflicts = NEW2(nstates, char);
shiftset = NEW2(tokensetsize, unsigned);
lookaheadset = NEW2(tokensetsize, unsigned);
any_conflicts = 0;
for (i = 0; i < nstates; i++)
set_conflicts(i);
}
set_conflicts(state)
int state;
{
register int i;
register int k;
register shifts *shiftp;
register unsigned *fp2;
register unsigned *fp3;
register unsigned *fp4;
register unsigned *fp1;
register int symbol;
for (i = 0; i < tokensetsize; i++)
lookaheadset[i] = 0;
shiftp = shift_table[state];
if (shiftp)
{
k = shiftp->nshifts;
for (i = 0; i < k; i++)
{
symbol = accessing_symbol[shiftp->shifts[i]];
if (ISVAR(symbol)) break;
SETBIT(lookaheadset, symbol);
}
}
k = lookaheads[state + 1];
fp4 = lookaheadset + tokensetsize;
for (i = lookaheads[state]; i < k; i++)
{
fp1 = LA + i * tokensetsize;
fp2 = fp1;
fp3 = lookaheadset;
while (fp3 < fp4)
{
if (*fp2++ & *fp3++)
{
conflicts[state] = 1;
any_conflicts = 1;
return;
}
}
fp2 = fp1;
fp3 = lookaheadset;
while (fp3 < fp4)
*fp3++ |= *fp2++;
}
}
conflict_log()
{
register int i;
src_total = 0;
rrc_total = 0;
for (i = 0; i < nstates; i++)
{
if (conflicts[i])
{
count_sr_conflicts(i);
count_rr_conflicts(i);
src_total += src_count;
rrc_total += rrc_count;
}
}
}
verbose_conflict_log()
{
register int i;
src_total = 0;
rrc_total = 0;
for (i = 0; i < nstates; i++)
{
if (conflicts[i])
{
count_sr_conflicts(i);
count_rr_conflicts(i);
src_total += src_count;
rrc_total += rrc_count;
fprintf(verbose_file, "State %d contains", i);
if (src_count == 1)
fprintf(verbose_file, " 1 shift/reduce conflict");
else if (src_count > 1)
fprintf(verbose_file, " %d shift/reduce conflicts", src_count);
if (src_count > 0 && rrc_count > 0)
fprintf(verbose_file, " and");
if (rrc_count == 1)
fprintf(verbose_file, " 1 reduce/reduce conflict");
else if (rrc_count > 1)
fprintf(verbose_file, " %d reduce/reduce conflicts", rrc_count);
putc('.', verbose_file);
putc('\n', verbose_file);
}
}
total_conflicts();
}
count_sr_conflicts(state)
int state;
{
register int i;
register int k;
register int mask;
register shifts *shiftp;
register unsigned *fp1;
register unsigned *fp2;
register unsigned *fp3;
register int symbol;
src_count = 0;
shiftp = shift_table[state];
if (!shiftp) return;
for (i = 0; i < tokensetsize; i++)
{
shiftset[i] = 0;
lookaheadset[i] = 0;
}
k = shiftp->nshifts;
for (i = 0; i < k; i++)
{
symbol = accessing_symbol[shiftp->shifts[i]];
if (ISVAR(symbol)) break;
SETBIT(shiftset, symbol);
}
k = lookaheads[state + 1];
fp3 = lookaheadset + tokensetsize;
for (i = lookaheads[state]; i < k; i++)
{
fp1 = LA + i * tokensetsize;
fp2 = lookaheadset;
while (fp2 < fp3)
*fp2++ |= *fp1++;
}
fp1 = shiftset;
fp2 = lookaheadset;
while (fp2 < fp3)
*fp2++ &= *fp1++;
mask = 1;
fp2 = lookaheadset;
for (i = 0; i < ntokens; i++)
{
if (mask & *fp2)
src_count++;
mask <<= 1;
if (mask == 0)
{
mask = 1;
fp2++;
}
}
}
count_rr_conflicts(state)
int state;
{
register int i;
register int j;
register int count;
register unsigned mask;
register unsigned *baseword;
register unsigned *wordp;
register int m;
register int n;
rrc_count = 0;
m = lookaheads[state];
n = lookaheads[state + 1];
if (n - m < 2) return;
mask = 1;
baseword = LA + m * tokensetsize;
for (i = 0; i < ntokens; i++)
{
wordp = baseword;
count = 0;
for (j = m; j < n; j++)
{
if (mask & *wordp)
count++;
wordp += tokensetsize;
}
if (count >= 2) rrc_count++;
mask <<= 1;
if (mask == 0)
{
mask = 1;
baseword++;
}
}
}
finalize_conflicts()
{
FREE(conflicts);
FREE(shiftset);
FREE(lookaheadset);
}